/*
 * @Descripttion: 
 * @version: 
 * @Author: spring
 * @Date: 2022-04-17 11:19:36
 * @LastEditors: spring
 * @LastEditTime: 2022-04-17 11:35:57
 */
import { multipleState } from '../useReducerStore';
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';

const config: AxiosRequestConfig = {
  timeout: 10000,
  headers: {
    'X-Requested-With': 'xmlhttprequest',
    'token': ''
  }
};
const instance = axios.create(config);

// 请求拦截器
instance.interceptors.request.use(
  (config: AxiosRequestConfig) => {
    const state = multipleState;

    Object.assign(config.headers, { token: state.auth.token });

    return config;
  },
  (error:any) => {
    return Promise.reject(error);
  }
);

// 响应拦截器
instance.interceptors.response.use(
  (response: AxiosResponse<any>) => {
    const res = response.data;

    if (res.code === 200) {
      return res;
    }

    console.log('Code Not 200:', response);
    return response;
  },
  (error:any) => {
    return Promise.reject(error);
  }
);

export default instance;
